<template>
<div class="deptList">
  <h4>欢迎来到部门</h4>
  <el-button type="primary" @click="handleEdit(null)">新增</el-button>
  <el-button type="danger" @click="handleDeleteAll">批删</el-button>


  <el-table
    :data="tableData"
    stripe
    style="width: 100%"
    row-key="deptId"
    :tree-props="{children: 'childMenus', hasChildren: 'hasChildren'}"
    @selection-change="handleSelectionChange"
  >
<!--      <el-table-column>
        <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
      </el-table-column>-->
      <el-table-column type="selection" width="55" />
      <el-table-column
        prop="deptId"
        label="部门ID"
        sortable
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="部门名称"
        sortable
        width="180">
      </el-table-column>
      <el-table-column
        prop="parentName"
        label="上级部门">
      </el-table-column>
      <el-table-column
        prop="contacts"
        label="联系人">
      </el-table-column>
      <el-table-column
        prop="orderNum"
        label="排序号">
      </el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          size="mini"
          @click="handleEdit(scope.row)">编辑</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete(scope.row)">删除</el-button>
      </template>
    </el-table-column>
    </el-table>
<!--////////////=============分页开始===========/////////////-->
  <el-pagination
    background
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    :current-page="current"
    :page-sizes="[10, 20, 25, 30]"
    :page-size="size"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total">
  </el-pagination>
<!--////////////=============分页结束===========/////////////-->



<!--/////////=========添加开始=======/////////-->
  <el-dialog :title="title" :visible.sync="dialogFormVisible">
    <el-form :model="form">

      <el-header v-model="form.deptId"></el-header>

      <el-form-item label="部门名称" :label-width="formLabelWidth" >
        <el-input v-model="form.name" autocomplete="off" placeholder="部门名称"></el-input>
      </el-form-item>

      <el-form-item label="上级部门" :label-width="formLabelWidth"  >
        <el-input v-model="form.parentName" autocomplete="off"  placeholder="上级部门" readonly @focus="centerDialogVisible=true"></el-input>
<!--        <el-input v-model="form.parentId" autocomplete="off" v-show="form.parentId"></el-input>-->
            <el-header v-model="form.parentId" autocomplete="off"></el-header>
        <el-dialog
          title="提示"
          :visible.sync="centerDialogVisible"
          width="30%"
          center>
          <el-tree :data="data" :props="DeptdefaultProps" @node-click="handleNodeClick"></el-tree>
          <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="centerDialogVisible = false">确 定</el-button>
  </span>
        </el-dialog>
      </el-form-item>
      <el-form-item label="联系人" :label-width="formLabelWidth" >
        <el-input v-model="form.contacts" autocomplete="off"  placeholder="联系人"></el-input>
      </el-form-item>

      <el-form-item label="排序号" :label-width="formLabelWidth" >
      <el-input v-model="form.orderNum" autocomplete="off"  placeholder="序号"></el-input>
<!--        <el-input-number v-model="form.orderNum" autocomplete="off"  :min="1" :max="100" ></el-input-number>-->
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="addAndModify">确 定</el-button>
    </div>
  </el-dialog>

<!--/////////=========添加结束=======/////////-->
</div>
</template>

<script>

import axios from "axios";

export default {
  data() {
    return {
      tableData: [],
      current:1,
      size:10,
      total:0,
      dialogFormVisible: false,
      centerDialogVisible:false,
      title:'添加操作',
      form:{},
      formLabelWidth: '120px',
      DeptdefaultProps:{
        value:"deptId",
        children: 'childMenus',
        label: 'name'
      },
      data:[],
      multipleSelection: [],

    }
  },
  created() {
    this.inifData()
    this.inifTree()
  },
  methods: {
    inifData() {
      axios.get('http://localhost:8200/sys/dept/list', {
        params: {
          pageNum: this.current,
          pageSize: this.size
        }
      }).then((res) => {
        this.tableData = res.data.list
        this.total = res.data.total
      })
    },
    handleSizeChange(val) {
      this.size = val
      this.inifData()
    },
    handleCurrentChange(val) {
      this.current = val
      this.inifData()
    },
    //修改and添加表单
    handleEdit(row) {
        if (row != null){
            this.title="修改操作"
          this.form = row;
        this.dialogFormVisible=true;
        }else {
          this.title="添加操作"
          this.dialogFormVisible=true;
        }


    },
    //批删
    handleDeleteAll() {
     var ids = this.multipleSelection.join(",")
      this.$alert(ids)
      this.$confirm("确定删除吗?").then(res=>{
        axios.get('http://localhost:8200/sys/dept/delete', {
          params: {
            ids:ids
          }
        }).then((res) => {

          if (res) {
            this.$message({
              showClose: true,
              message: '成功删除一条数据',
              type: 'success'
            })

            this.inifData()
          }
        })
      })

    },
    //单删
    handleDelete(row) {

      this.$confirm("确定删除吗?").then(res=>{
        axios.get('http://localhost:8200/sys/dept/delete', {
          params: {
            ids:row.deptId
          }
        }).then((res) => {

          if (res) {
            this.$message({
              showClose: true,
              message: '成功删除一条数据',
              type: 'success'
            })

            this.inifData()
          }
        })
      })

    },
    /*树控件*/
    inifTree() {
      axios.get('http://localhost:8200/sys/dept/list', {
        params: {
          pageSize: '1000'
        }
      }).then((res) => {
        this.data = res.data.list

      })
    },
    //给树控件赋值
    handleNodeClick(data) {
        this.form.parentName=data.name
        this.form.parentId=data.deptId
        this.centerDialogVisible=false
    },
    addAndModify(){
      axios.post('http://localhost:8200/sys/dept/addEndUpdate',this.form).then((res) => {
        if (res){
          alert("操作成功!");
          this.inifData();
          this.dialogFormVisible = false
        }
      })
    },
    handleSelectionChange(vals) {
      this.multipleSelection = new Array()

      for(let val of vals){
        this.multipleSelection.push(val.deptId)
      }


    }

  }

}
</script>

<style scoped>

</style>
